﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraGrid;
using System.Drawing;
using System.Drawing.Printing;
using DevExpress.XtraGrid.Views.Grid;
using System.Windows.Forms;
using System.Data;

namespace CIRDiagnoseDevice
{
    class PrintRecord
    {
        GridView gridView; //打印数据的来源
        DataGridView dgv; // 打印数据的来源
        string deviceName; //CIR唯一标识名
        int talPage;           //总页总
        int perpageRow = 25;          //每页打印行数
        int endpageRow;        //最后一页的行数
        int currentpageindex = 1;  //当前页页码
        string testTime;       //测试时间
        string tester;          //测试人员
        Pen mypen = new Pen(Color.Black);
        Font font = new Font("宋体", 12f);
        Brush brush = new SolidBrush(Color.Black);
        Font headFont = new Font("宋体", 14.5f, FontStyle.Bold);
        int rowGap = 30;    //行高
        int PrintPageWidth = 827; //打印页面宽度
        int PrintPageHeight = 1169; // 打印页面高度
        int topmargin = 50; //顶边距
        int buttommargin = 50; //底边距
        int leftmargin = 50; //左边距
        int rightmargin = 50; //右边距
        int colWidth;            //列宽度
        int printedLine;      // 已经打印的行数
        float pageHeight = 0;
        private int recHeight = 0; //绘制字符串的矩形初始高度
        private int recWidth = 0; // 绘制字符串的矩形初始宽度
        private int recX = 0;       // 绘制字符串的矩形初始X坐标
        private int recY = 0;      // 绘制字符串的矩形初始Y坐标
        private bool haveToPageEnd;  //是否到一页的结尾标志
        private DeviceInfo deviceInfo;
        private bool IsHaveCheck;

        public PrintRecord(GridView gridView, string deviceName, string testTime, string tester, DeviceInfo deviceInfo, DataGridView dgv, bool isHaveCheck)
        {
            this.gridView = gridView;
            this.deviceName = deviceName;
            this.tester = tester;
            this.testTime = testTime;
            this.deviceInfo = deviceInfo;
            this.dgv = dgv;
            this.IsHaveCheck = isHaveCheck;
        }

        public void PrintTestRecord(PrintPageEventArgs e)
        {
            // if (gridView.RowCount > 0)
            {
                if (currentpageindex == 1)
                {
                    rowGap = Convert.ToInt32((PrintPageHeight - topmargin - buttommargin - 5 * perpageRow) / perpageRow) + 3;
                    //绘制标题与测试时间、人员信息
                    e.Graphics.DrawString(deviceName + "设备测试记录", headFont, brush, 320, topmargin + 10);
                    e.Graphics.DrawString("测试时间:   " + testTime, font, brush, 100, topmargin + 65);
                    e.Graphics.DrawString(("测试员:   " + tester), font, brush, PrintPageWidth - leftmargin - rightmargin * 4, topmargin + 65);
                    e.Graphics.DrawString("机车配属", font, brush, leftmargin + 5, topmargin + 105);
                    e.Graphics.DrawLine(mypen, leftmargin + 78, topmargin + 100, leftmargin + 78, topmargin + 100 + rowGap); //
                    e.Graphics.DrawString(deviceInfo.Cab_bur + " 铁路局 " + deviceInfo.Cab_seg + " 段 " + deviceInfo.Cab_pl + " 所  ", font, brush, leftmargin + 80, topmargin + 105);
                    e.Graphics.DrawLine(mypen, leftmargin, topmargin + 100 + rowGap, PrintPageWidth - leftmargin - rightmargin, topmargin + 100 + rowGap);
                    e.Graphics.DrawLine(mypen, leftmargin + 78, topmargin + 100 + rowGap, leftmargin + 78, topmargin + 100 + rowGap * 2);
                    e.Graphics.DrawString("机车端别", font, brush, leftmargin + 5, topmargin + 100 + rowGap + 5);
                    e.Graphics.DrawLine(mypen, leftmargin, topmargin + 100 + rowGap * 2, PrintPageWidth - leftmargin - rightmargin, topmargin + 100 + rowGap * 2);
                    e.Graphics.DrawString(deviceInfo.Cab_db.ToString(), font, brush, leftmargin + 82, topmargin + 100 + rowGap + 5);
                    e.Graphics.DrawLine(mypen, leftmargin + 138, topmargin + 100 + rowGap, leftmargin + 138, topmargin + 100 + rowGap * 2);
                    e.Graphics.DrawString("机车编号", font, brush, leftmargin + 143, topmargin + 100 + rowGap + 5);
                    e.Graphics.DrawLine(mypen, leftmargin + 216, topmargin + 100 + rowGap, leftmargin + 216, topmargin + 100 + rowGap * 2);
                    e.Graphics.DrawString(deviceInfo.Cab_num, font, brush, leftmargin + 220, topmargin + 100 + rowGap + 5);
                    e.Graphics.DrawLine(mypen, leftmargin + 370, topmargin + 100 + rowGap, leftmargin + 370, topmargin + 100 + rowGap * 2);
                    e.Graphics.DrawString("机车修程", font, brush, leftmargin + 375, topmargin + 105 + rowGap * 1);
                    e.Graphics.DrawLine(mypen, leftmargin + 450, topmargin + 100 + rowGap, leftmargin + 450, topmargin + 100 + rowGap * 2);
                    e.Graphics.DrawString(deviceInfo.Cab_xc, font, brush, leftmargin + 455, topmargin + 105 + rowGap);
                    e.Graphics.DrawString("CIR设备配属", font, brush, leftmargin + 5, topmargin + 105 + rowGap * 2);
                    e.Graphics.DrawLine(mypen, leftmargin + 115, topmargin + 100 + rowGap * 2, leftmargin + 115, topmargin + 100 + rowGap * 3);
                    e.Graphics.DrawLine(mypen, leftmargin, topmargin + 100 + rowGap * 3, PrintPageWidth - leftmargin - rightmargin, topmargin + 100 + rowGap * 3);
                    e.Graphics.DrawString(deviceInfo.Dev_bur + " 铁路局 " + deviceInfo.Dev_seg + " 段 " + deviceInfo.Dev_ws + " 车间 " + deviceInfo.Dev_wa + " 工区  ",
                                                         font, brush, leftmargin + 120, topmargin + 105 + rowGap * 2);
                    e.Graphics.DrawString("CIR设备型号", font, brush, leftmargin + 5, topmargin + 105 + rowGap * 3);
                    e.Graphics.DrawLine(mypen, leftmargin + 115, topmargin + 100 + rowGap * 3, leftmargin + 115, topmargin + 100 + rowGap * 4);
                    e.Graphics.DrawString(deviceInfo.Dev_mod, font, brush, leftmargin + 120, topmargin + 105 + rowGap * 3);
                    e.Graphics.DrawLine(mypen, leftmargin + 232, topmargin + 100 + rowGap * 3, leftmargin + 232, topmargin + 100 + rowGap * 4);
                    e.Graphics.DrawString("CIR设备生产商", font, brush, leftmargin + 235, topmargin + 105 + rowGap * 3);
                    e.Graphics.DrawLine(mypen, leftmargin + 362, topmargin + 100 + rowGap * 3, leftmargin + 362, topmargin + 100 + rowGap * 4);
                    e.Graphics.DrawString(deviceInfo.Dev_man, font, brush, leftmargin + 365, topmargin + 105 + rowGap * 3);
                    e.Graphics.DrawLine(mypen, leftmargin, topmargin + 100 + rowGap * 4, PrintPageWidth - leftmargin - rightmargin, topmargin + 100 + rowGap * 4);
                    e.Graphics.DrawString("CIR设备编号", font, brush, leftmargin + 5, topmargin + 105 + rowGap * 4);
                    e.Graphics.DrawLine(mypen, leftmargin + 115, topmargin + 100 + rowGap * 4, leftmargin + 115, topmargin + 100 + rowGap * 5);
                    e.Graphics.DrawString(deviceInfo.Dev_num, font, brush, leftmargin + 120, topmargin + 105 + rowGap * 4);
                    e.Graphics.DrawLine(mypen, leftmargin + 275, topmargin + 100 + rowGap * 4, leftmargin + 275, topmargin + 100 + rowGap * 5);
                    e.Graphics.DrawString("CIR设备修程", font, brush, leftmargin + 280, topmargin + 105 + rowGap * 4);
                    e.Graphics.DrawLine(mypen, leftmargin + 390, topmargin + 100 + rowGap * 4, leftmargin + 390, topmargin + 100 + rowGap * 5);
                    e.Graphics.DrawString(deviceInfo.Cab_xc, font, brush, leftmargin + 395, topmargin + 105 + rowGap * 4);
                    e.Graphics.DrawLine(mypen, leftmargin, topmargin + 100 + rowGap * 5, PrintPageWidth - leftmargin - rightmargin, topmargin + 100 + rowGap * 5);
                    bool havePages = DrawTestGrid(6, currentpageindex, e);
                    if (havePages)
                        e.HasMorePages = true;
                }
                else
                {
                    bool havePages = DrawTestGrid(0, currentpageindex, e);
                    if (havePages)
                        e.HasMorePages = true;
                }
                //绘制边框
                e.Graphics.DrawLine(mypen, leftmargin, topmargin + 100, PrintPageWidth - leftmargin - rightmargin, topmargin + 100);// 上边框
                e.Graphics.DrawLine(mypen, leftmargin, topmargin + 100, leftmargin, PrintPageHeight - topmargin - buttommargin); //左边框
                e.Graphics.DrawLine(mypen, leftmargin, PrintPageHeight - topmargin - buttommargin, PrintPageWidth - leftmargin - rightmargin,
                                                PrintPageHeight - topmargin - buttommargin); //下边框
                e.Graphics.DrawLine(mypen, PrintPageWidth - leftmargin - rightmargin, topmargin + 100, PrintPageWidth - leftmargin - rightmargin,
                                                PrintPageHeight - topmargin - buttommargin);// 右边框

            }
        }

        private bool DrawTestGrid(int startdrawIndex, int currentPage, PrintPageEventArgs e)
        {
            // int startDataIndex =  currentPage * perpageRow - perpageRow;
            endpageRow = currentPage * perpageRow;
            colWidth = Convert.ToInt32((PrintPageWidth - leftmargin - rightmargin - 10) / 6);
            StringFormat format = new StringFormat();
            format.Alignment = StringAlignment.Center;
            format.LineAlignment = StringAlignment.Center;
            Rectangle rec = new Rectangle();
            int initY = rowGap * (startdrawIndex);
            recWidth = colWidth;
            recY = topmargin + 115 + initY;
            if (currentPage == 1)
            {
                if (gridView != null)
                {
                    for (int k = 0; k < gridView.Columns.Count; k++)
                    {
                        if (k == 0 || k == 1)
                            e.Graphics.DrawString(gridView.Columns[k].FieldName, font, brush, leftmargin + k * colWidth + 10, topmargin + 75 + rowGap * (startdrawIndex));
                        else if (k == 2 || k == 3)
                            e.Graphics.DrawString(gridView.Columns[k].FieldName, font, brush, leftmargin + k * colWidth - 20, topmargin + 75 + rowGap * (startdrawIndex));
                        else
                            e.Graphics.DrawString(gridView.Columns[k].FieldName, font, brush, leftmargin - 30 + k * colWidth, topmargin + 75 + rowGap * (startdrawIndex));
                    }
                    printedLine = 0;
                    haveToPageEnd = false;
                    for (int i = 0; i < gridView.RowCount; i++)
                    {
                        for (int j = 0; j < gridView.Columns.Count; j++)
                        {
                            if ((PrintPageWidth - leftmargin - rightmargin - j * colWidth) < colWidth)
                                recWidth = PrintPageWidth - leftmargin - rightmargin - j * colWidth;
                            if (recY < e.MarginBounds.Height)
                            {
                                if (j == 0)
                                    recX = leftmargin + 5 + j * colWidth;  // 每个单元格开始和X坐标
                                else if (j == (gridView.Columns.Count - 2) || j == (gridView.Columns.Count - 1))
                                    recX = leftmargin + 5 + j * colWidth - 60;
                                else
                                    recX = leftmargin + 5 + j * colWidth - 40;
                              
                                recHeight = 80;                      //每行的行高
                                rec = new Rectangle(recX, recY, recWidth, recHeight);
                                object str = gridView.GetRowCellValue(i, gridView.Columns[j]);
                                string valuStr = string.Empty;
                                if (str != null)
                                {
                                    valuStr = str.ToString();
                                }
                                e.Graphics.DrawString(valuStr, font, brush, rec, format);
                                e.Graphics.DrawLine(mypen, leftmargin, recY - 3, PrintPageWidth - leftmargin - rightmargin, recY - 3);
                            }
                            else
                            {
                                haveToPageEnd = true;
                                break;
                            }
                        }
                        if (haveToPageEnd)
                            break;
                        else
                        {
                            recY = recY + (recHeight); //每行的Y 坐标
                            printedLine++;
                        }
                    }
                    if (printedLine < gridView.RowCount)
                    {
                        currentpageindex++;
                        e.Graphics.DrawLine(mypen, leftmargin + colWidth, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth, PrintPageHeight - topmargin - buttommargin);
                        e.Graphics.DrawLine(mypen, leftmargin + colWidth * 2 - 30, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth * 2 - 30, PrintPageHeight - topmargin - buttommargin);
                        e.Graphics.DrawLine(mypen, leftmargin + colWidth * 3 - 25, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth * 3 - 25, PrintPageHeight - topmargin - buttommargin);
                        e.Graphics.DrawLine(mypen, leftmargin + colWidth * 4 - 40, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth * 4 - 40, PrintPageHeight - topmargin - buttommargin);
                        e.Graphics.DrawLine(mypen, leftmargin + colWidth * 5 - 60, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth * 5 - 60, PrintPageHeight - topmargin - buttommargin);
                        haveToPageEnd = false;
                        return true;
                    }
                    else
                    {
                        e.Graphics.DrawLine(mypen, leftmargin + colWidth, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth, PrintPageHeight - topmargin - buttommargin);
                        e.Graphics.DrawLine(mypen, leftmargin + colWidth * 2 - 30, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth * 2 - 30, PrintPageHeight - topmargin - buttommargin);
                        e.Graphics.DrawLine(mypen, leftmargin + colWidth * 3 - 25, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth * 3 - 25, PrintPageHeight - topmargin - buttommargin);
                        e.Graphics.DrawLine(mypen, leftmargin + colWidth * 4 - 40, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth * 4 - 40, PrintPageHeight - topmargin - buttommargin);
                        e.Graphics.DrawLine(mypen, leftmargin + colWidth * 5 - 60, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth * 5 - 60, PrintPageHeight - topmargin - buttommargin);
                        if (recY < e.MarginBounds.Height)
                            e.Graphics.DrawLine(mypen, leftmargin, recY - 3, PrintPageWidth - leftmargin - rightmargin, recY - 3);
                        haveToPageEnd = true;
                        return false;
                    }
                }
                else if (dgv != null)
                {
                    colWidth = Convert.ToInt32((PrintPageWidth - leftmargin - rightmargin - 10) / dgv.Columns.Count);
                    int startColumnIndex = 0;
                    int m = 0;
                    if (IsHaveCheck)
                        startColumnIndex = 1;
                    for (int k = startColumnIndex; k < dgv.Columns.Count; k++)
                    {
                        if (m == 0 || m == 1)
                            e.Graphics.DrawString(dgv.Columns[k].HeaderText, font, brush, leftmargin + m * colWidth + 10, topmargin + 75 + rowGap * (startdrawIndex));
                        else if (m == 2)
                            e.Graphics.DrawString(dgv.Columns[k].HeaderText, font, brush, leftmargin + m * colWidth + 40, topmargin + 75 + rowGap * (startdrawIndex));
                        else if (m == 3)
                            e.Graphics.DrawString(dgv.Columns[k].HeaderText, font, brush, leftmargin + m * colWidth - 5, topmargin + 75 + rowGap * (startdrawIndex));
                        else if (m == 4)
                            e.Graphics.DrawString(dgv.Columns[k].HeaderText, font, brush, leftmargin + m * colWidth + 60, topmargin + 75 + rowGap * (startdrawIndex));
                        else if (m == 5)
                            e.Graphics.DrawString(dgv.Columns[k].HeaderText, font, brush, leftmargin + m * colWidth + 70, topmargin + 75 + rowGap * (startdrawIndex));
                        else
                            e.Graphics.DrawString(dgv.Columns[k].HeaderText, font, brush, leftmargin + m * colWidth - 30, topmargin + 75 + rowGap * (startdrawIndex));
                        m++;
                    }
                    printedLine = 0;
                    haveToPageEnd = false;
                    for (int i = 0; i < dgv.Rows.Count; i++)
                    {
                        bool result = true;
                        if (dgv.Columns.Count > 3)
                            result = dgv.Rows[i].Cells["测试结果"].Value.ToString() != "" ? true : false;
                        else
                            result = dgv.Rows[i].Cells["试验结果"].Value.ToString() != "" ? true : false;
                        if (result)
                        {
                            m = 0;
                            for (int j = startColumnIndex; j < dgv.Columns.Count; j++)
                            {

                                if ((PrintPageWidth - leftmargin - rightmargin - m * colWidth) < colWidth)
                                    recWidth = PrintPageWidth - leftmargin - rightmargin - (dgv.Columns.Count - 1) * colWidth;
                                if (recY < e.MarginBounds.Height)
                                {
                                    if (m == 0)
                                        recX = leftmargin + m * colWidth;  // 每个单元格开始和X坐标
                                    else if (m == 1)
                                        recX = leftmargin + m * colWidth + 10;
                                    else if (m == 2)
                                        recX = leftmargin + 5 + m * colWidth + 10;
                                    else if (m == 3)
                                        recX = leftmargin + 5 + m * colWidth + 5;
                                    else if (m == 4)
                                        recX = leftmargin + 5 + m * colWidth + 70;
                                    else if (m == 5)
                                        recX = leftmargin + 5 + m * colWidth + 60;
                                    recHeight = dgv.Rows[i].Height > 50 ? dgv.Rows[i].Height : 50;
                                  //  recHeight = 80;                      //每行的行高
                                    rec = new Rectangle(recX, recY, recWidth, recHeight);
                                    object str = dgv.Rows[i].Cells[j].Value;
                                    string valuStr = string.Empty;
                                    if (str != null)
                                    {
                                        valuStr = str.ToString();
                                    }
                                    e.Graphics.DrawString(valuStr, font, brush, rec, format);
                                    e.Graphics.DrawLine(mypen, leftmargin, recY - 3, PrintPageWidth - leftmargin - rightmargin, recY - 3);
                                }
                                else
                                {
                                    haveToPageEnd = true;
                                    break;
                                }
                                m++;
                            }
                            if (haveToPageEnd)
                                break;
                            else
                            {
                                recY = recY + (recHeight); //每行的Y 坐标
                                printedLine++;
                            }
                        }
                        else
                            continue;
                    }
                    if (printedLine < dgv.Rows.Count)
                    {
                        currentpageindex++;
                        e.Graphics.DrawLine(mypen, leftmargin + colWidth + 10, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth + 10, PrintPageHeight - topmargin - buttommargin);
                        e.Graphics.DrawLine(mypen, leftmargin + colWidth * 2 + 40, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth * 2 + 40, PrintPageHeight - topmargin - buttommargin);
                        e.Graphics.DrawLine(mypen, leftmargin + colWidth * 3 - 5, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth * 3 - 5, PrintPageHeight - topmargin - buttommargin);
                        e.Graphics.DrawLine(mypen, leftmargin + colWidth * 4 + 60, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth * 4 + 60, PrintPageHeight - topmargin - buttommargin);
                        e.Graphics.DrawLine(mypen, leftmargin + colWidth * 5 + 70, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth * 5 + 70, PrintPageHeight - topmargin - buttommargin);
                        haveToPageEnd = false;
                        return true;
                    }
                    else
                    {
                        e.Graphics.DrawLine(mypen, leftmargin + colWidth + 10, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth + 10, PrintPageHeight - topmargin - buttommargin);
                        e.Graphics.DrawLine(mypen, leftmargin + colWidth * 2 + 40, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth * 2 + 40, PrintPageHeight - topmargin - buttommargin);
                        e.Graphics.DrawLine(mypen, leftmargin + colWidth * 3 - 5, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth * 3 - 5, PrintPageHeight - topmargin - buttommargin);
                        e.Graphics.DrawLine(mypen, leftmargin + colWidth * 4 + 60, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth * 4 + 60, PrintPageHeight - topmargin - buttommargin);
                        e.Graphics.DrawLine(mypen, leftmargin + colWidth * 5 + 70, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth * 5 + 70, PrintPageHeight - topmargin - buttommargin);
                        if (recY < e.MarginBounds.Height)
                            e.Graphics.DrawLine(mypen, leftmargin, recY - 3, PrintPageWidth - leftmargin - rightmargin, recY - 3);
                        haveToPageEnd = true;
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
            else
            {
                if (gridView != null)
                {
                    for (int k = 0; k < gridView.Columns.Count; k++)
                    {
                        if (k == 0 || k == 1)
                            e.Graphics.DrawString(gridView.Columns[k].FieldName, font, brush, leftmargin + k * colWidth + 10, topmargin + 105);
                        else if (k == 2 || k == 3)
                            e.Graphics.DrawString(gridView.Columns[k].FieldName, font, brush, leftmargin + k * colWidth - 20, topmargin + 105);
                        else
                            e.Graphics.DrawString(gridView.Columns[k].FieldName, font, brush, leftmargin - 30 + k * colWidth, topmargin + 105);
                    }
                    recY += 30;
                    for (int i = printedLine; i < gridView.RowCount; i++)
                    {
                        for (int j = 0; j < gridView.Columns.Count; j++)
                        {
                            if ((PrintPageWidth - leftmargin - rightmargin - j * colWidth) < colWidth)
                                recWidth = PrintPageWidth - leftmargin - rightmargin - j * colWidth;
                            if (recY < e.MarginBounds.Height)
                            {
                                if (j == 0)
                                    recX = leftmargin + 5 + j * colWidth;  // 每个单元格开始和X坐标
                                else if (j == (gridView.Columns.Count - 2) || j == (gridView.Columns.Count - 1))
                                    recX = leftmargin + 5 + j * colWidth - 60;
                                else
                                    recX = leftmargin + 5 + j * colWidth - 40;
                                recHeight = 80;
                                rec = new Rectangle(recX, recY, recWidth, recHeight);
                                string str = gridView.GetRowCellValue(i, gridView.Columns[j]).ToString();
                                e.Graphics.DrawString(str, font, brush, rec, format);
                                e.Graphics.DrawLine(mypen, leftmargin, recY - 3, PrintPageWidth - leftmargin - rightmargin, recY - 3);
                            }
                            else
                            {
                                haveToPageEnd = true;
                                break;
                            }
                        }
                        if (haveToPageEnd)
                            break;
                        else
                        {
                            recY += recHeight;
                            printedLine++;
                        }
                    }
                    if (recY < e.MarginBounds.Height)
                    {
                        e.Graphics.DrawLine(mypen, leftmargin, recY - 3, PrintPageWidth - leftmargin - rightmargin, recY - 3);
                    }
                    //绘制每列列线
                    e.Graphics.DrawLine(mypen, leftmargin + colWidth, topmargin + 100 + rowGap * (startdrawIndex), leftmargin + colWidth, PrintPageHeight - topmargin - buttommargin);
                    e.Graphics.DrawLine(mypen, leftmargin + colWidth * 2 - 30, topmargin + 100 + rowGap * (startdrawIndex), leftmargin + colWidth * 2 - 30, PrintPageHeight - topmargin - buttommargin);
                    e.Graphics.DrawLine(mypen, leftmargin + colWidth * 3 - 25, topmargin + 100 + rowGap * (startdrawIndex), leftmargin + colWidth * 3 - 25, PrintPageHeight - topmargin - buttommargin);
                    e.Graphics.DrawLine(mypen, leftmargin + colWidth * 4 - 40, topmargin + 100 + rowGap * (startdrawIndex), leftmargin + colWidth * 4 - 40, PrintPageHeight - topmargin - buttommargin);
                    e.Graphics.DrawLine(mypen, leftmargin + colWidth * 5 - 60, topmargin + 100 + rowGap * (startdrawIndex), leftmargin + colWidth * 5 - 60, PrintPageHeight - topmargin - buttommargin);
                    if (printedLine < gridView.RowCount - 1)
                    {
                        currentpageindex++;
                        haveToPageEnd = false;
                        return true;
                    }
                    else
                    {
                        haveToPageEnd = true;
                        return false;
                    }
                }
                else if (dgv != null)
                {
                    colWidth = Convert.ToInt32((PrintPageWidth - leftmargin - rightmargin - 10) / dgv.Columns.Count);
                    int m = 0;
                    int startColumnIndex = 0;
                    if (IsHaveCheck)
                        startColumnIndex = 1;
                    for (int k = startColumnIndex; k < dgv.Columns.Count; k++)
                    {
                        if (m == 0 || m == 1)
                            e.Graphics.DrawString(dgv.Columns[k].HeaderText, font, brush, leftmargin + m * colWidth + 10, topmargin + 105);
                        else if (m == 2)
                            e.Graphics.DrawString(dgv.Columns[k].HeaderText, font, brush, leftmargin + m * colWidth + 40, topmargin + 105);
                        else if (m == 3)
                            e.Graphics.DrawString(dgv.Columns[k].HeaderText, font, brush, leftmargin + m * colWidth - 5, topmargin + 105);
                        else if (m == 4)
                            e.Graphics.DrawString(dgv.Columns[k].HeaderText, font, brush, leftmargin + m * colWidth + 60, topmargin + 105);
                        else if (m == 5)
                            e.Graphics.DrawString(dgv.Columns[k].HeaderText, font, brush, leftmargin + m * colWidth + 70, topmargin + 105);
                        else
                            e.Graphics.DrawString(dgv.Columns[k].HeaderText, font, brush, leftmargin + m * colWidth - 30, topmargin + 105);
                        m++;
                    }
                    recY += 30;
                    for (int i = printedLine; i < dgv.Rows.Count; i++)
                    {
                        bool result = true;
                        if (dgv.Columns.Count > 3)
                            result = dgv.Rows[i].Cells["测试结果"].Value.ToString() != "" ? true : false;
                        else
                            result = dgv.Rows[i].Cells["试验结果"].Value.ToString() != "" ? true : false;
                        if (result)
                        {
                            m = 0;
                            for (int j = startColumnIndex; j < dgv.Columns.Count; j++)
                            {

                                if ((PrintPageWidth - leftmargin - rightmargin - m * colWidth) < colWidth)
                                    recWidth = PrintPageWidth - leftmargin - rightmargin - m * colWidth;
                                if (recY < e.MarginBounds.Height)
                                {
                                    if (m == 0)
                                        recX = leftmargin + m * colWidth;  // 每个单元格开始和X坐标
                                    else if (m == 1)
                                        recX = leftmargin + m * colWidth + 10;
                                    else if (m == 2)
                                        recX = leftmargin + 5 + m * colWidth + 10;
                                    else if (m == 3)
                                        recX = leftmargin + 5 + m * colWidth + 5;
                                    else if (m == 4)
                                        recX = leftmargin + 5 + m * colWidth + 70;
                                    else if (m == 5)
                                        recX = leftmargin + 5 + m * colWidth + 60;
                                    recHeight = dgv.Rows[i].Height > 50 ? dgv.Rows[i].Height : 50;
                                   // recHeight = 80;
                                    rec = new Rectangle(recX, recY, recWidth, recHeight);
                                    string str = dgv.Rows[i].Cells[j].Value.ToString();
                                    e.Graphics.DrawString(str, font, brush, rec, format);
                                    e.Graphics.DrawLine(mypen, leftmargin, recY - 3, PrintPageWidth - leftmargin - rightmargin, recY - 3);
                                }
                                else
                                {
                                    haveToPageEnd = true;
                                    break;
                                }
                                m++;
                            }
                            if (haveToPageEnd)
                                break;
                            else
                            {
                                recY += recHeight;
                                printedLine++;
                            }
                        }
                        else
                            continue;
                    }
                    if (recY < e.MarginBounds.Height)
                    {
                        e.Graphics.DrawLine(mypen, leftmargin, recY - 3, PrintPageWidth - leftmargin - rightmargin, recY - 3);
                    }
                    //绘制每列列线
                    e.Graphics.DrawLine(mypen, leftmargin + colWidth + 10, topmargin + 100 + rowGap * (startdrawIndex), leftmargin + colWidth + 10, PrintPageHeight - topmargin - buttommargin);
                    e.Graphics.DrawLine(mypen, leftmargin + colWidth * 2 + 40, topmargin + 100 + rowGap * (startdrawIndex), leftmargin + colWidth * 2 + 40, PrintPageHeight - topmargin - buttommargin);
                    e.Graphics.DrawLine(mypen, leftmargin + colWidth * 3 - 5, topmargin + 100 + rowGap * (startdrawIndex), leftmargin + colWidth * 3 - 5, PrintPageHeight - topmargin - buttommargin);
                    e.Graphics.DrawLine(mypen, leftmargin + colWidth * 4 + 60, topmargin + 100 + rowGap * (startdrawIndex), leftmargin + colWidth * 4 + 60, PrintPageHeight - topmargin - buttommargin);
                    e.Graphics.DrawLine(mypen, leftmargin + colWidth * 5 + 70, topmargin + 100 + rowGap * (startdrawIndex), leftmargin + colWidth * 5 + 70, PrintPageHeight - topmargin - buttommargin);
                    if (printedLine < dgv.Rows.Count - 1)
                    {
                        currentpageindex++;
                        haveToPageEnd = false;
                        return true;
                    }
                    else
                    {
                        haveToPageEnd = true;
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
        }

        public void PrintTotalTestResult(PrintPageEventArgs e)
        {
            if (currentpageindex == 1)
            {
                rowGap = Convert.ToInt32((PrintPageHeight - topmargin - buttommargin - 5 * perpageRow) / perpageRow) + 3;
                //绘制标题与测试时间、人员信息
                e.Graphics.DrawString(deviceName + "设备测试记录", headFont, brush, 320, topmargin + 10);
                e.Graphics.DrawString("测试时间:   " + testTime, font, brush, 100, topmargin + 65);
                e.Graphics.DrawString(("测试员:   " + tester), font, brush, PrintPageWidth - leftmargin - rightmargin * 4, topmargin + 65);
                e.Graphics.DrawString("机车配属", font, brush, leftmargin + 5, topmargin + 105);
                e.Graphics.DrawLine(mypen, leftmargin + 78, topmargin + 100, leftmargin + 78, topmargin + 100 + rowGap); //
                e.Graphics.DrawString(deviceInfo.Cab_bur + " 铁路局 " + deviceInfo.Cab_seg + " 段 " + deviceInfo.Cab_pl + " 所  ", font, brush, leftmargin + 80, topmargin + 105);
                e.Graphics.DrawLine(mypen, leftmargin, topmargin + 100 + rowGap, PrintPageWidth - leftmargin - rightmargin, topmargin + 100 + rowGap);
                e.Graphics.DrawLine(mypen, leftmargin + 78, topmargin + 100 + rowGap, leftmargin + 78, topmargin + 100 + rowGap * 2);
                e.Graphics.DrawString("机车端别", font, brush, leftmargin + 5, topmargin + 100 + rowGap + 5);
                e.Graphics.DrawLine(mypen, leftmargin, topmargin + 100 + rowGap * 2, PrintPageWidth - leftmargin - rightmargin, topmargin + 100 + rowGap * 2);
                e.Graphics.DrawString(deviceInfo.Cab_db.ToString(), font, brush, leftmargin + 82, topmargin + 100 + rowGap + 5);
                e.Graphics.DrawLine(mypen, leftmargin + 138, topmargin + 100 + rowGap, leftmargin + 138, topmargin + 100 + rowGap * 2);
                e.Graphics.DrawString("机车编号", font, brush, leftmargin + 143, topmargin + 100 + rowGap + 5);
                e.Graphics.DrawLine(mypen, leftmargin + 216, topmargin + 100 + rowGap, leftmargin + 216, topmargin + 100 + rowGap * 2);
                e.Graphics.DrawString(deviceInfo.Cab_num, font, brush, leftmargin + 220, topmargin + 100 + rowGap + 5);
                e.Graphics.DrawLine(mypen, leftmargin + 370, topmargin + 100 + rowGap, leftmargin + 370, topmargin + 100 + rowGap * 2);
                e.Graphics.DrawString("机车修程", font, brush, leftmargin + 375, topmargin + 105 + rowGap * 1);
                e.Graphics.DrawLine(mypen, leftmargin + 450, topmargin + 100 + rowGap, leftmargin + 450, topmargin + 100 + rowGap * 2);
                e.Graphics.DrawString(deviceInfo.Cab_xc, font, brush, leftmargin + 455, topmargin + 105 + rowGap);
                e.Graphics.DrawString("CIR设备配属", font, brush, leftmargin + 5, topmargin + 105 + rowGap * 2);
                e.Graphics.DrawLine(mypen, leftmargin + 115, topmargin + 100 + rowGap * 2, leftmargin + 115, topmargin + 100 + rowGap * 3);
                e.Graphics.DrawLine(mypen, leftmargin, topmargin + 100 + rowGap * 3, PrintPageWidth - leftmargin - rightmargin, topmargin + 100 + rowGap * 3);
                e.Graphics.DrawString(deviceInfo.Dev_bur + " 铁路局 " + deviceInfo.Dev_seg + " 段 " + deviceInfo.Dev_ws + " 车间 " + deviceInfo.Dev_wa + " 工区  ",
                                                     font, brush, leftmargin + 120, topmargin + 105 + rowGap * 2);
                e.Graphics.DrawString("CIR设备型号", font, brush, leftmargin + 5, topmargin + 105 + rowGap * 3);
                e.Graphics.DrawLine(mypen, leftmargin + 115, topmargin + 100 + rowGap * 3, leftmargin + 115, topmargin + 100 + rowGap * 4);
                e.Graphics.DrawString(deviceInfo.Dev_mod, font, brush, leftmargin + 120, topmargin + 105 + rowGap * 3);
                e.Graphics.DrawLine(mypen, leftmargin + 232, topmargin + 100 + rowGap * 3, leftmargin + 232, topmargin + 100 + rowGap * 4);
                e.Graphics.DrawString("CIR设备生产商", font, brush, leftmargin + 235, topmargin + 105 + rowGap * 3);
                e.Graphics.DrawLine(mypen, leftmargin + 362, topmargin + 100 + rowGap * 3, leftmargin + 362, topmargin + 100 + rowGap * 4);
                e.Graphics.DrawString(deviceInfo.Dev_man, font, brush, leftmargin + 365, topmargin + 105 + rowGap * 3);
                e.Graphics.DrawLine(mypen, leftmargin, topmargin + 100 + rowGap * 4, PrintPageWidth - leftmargin - rightmargin, topmargin + 100 + rowGap * 4);
                e.Graphics.DrawString("CIR设备编号", font, brush, leftmargin + 5, topmargin + 105 + rowGap * 4);
                e.Graphics.DrawLine(mypen, leftmargin + 115, topmargin + 100 + rowGap * 4, leftmargin + 115, topmargin + 100 + rowGap * 5);
                e.Graphics.DrawString(deviceInfo.Dev_num, font, brush, leftmargin + 120, topmargin + 105 + rowGap * 4);
                e.Graphics.DrawLine(mypen, leftmargin + 275, topmargin + 100 + rowGap * 4, leftmargin + 275, topmargin + 100 + rowGap * 5);
                e.Graphics.DrawString("CIR设备修程", font, brush, leftmargin + 280, topmargin + 105 + rowGap * 4);
                e.Graphics.DrawLine(mypen, leftmargin + 390, topmargin + 100 + rowGap * 4, leftmargin + 390, topmargin + 100 + rowGap * 5);
                e.Graphics.DrawString(deviceInfo.Cab_xc, font, brush, leftmargin + 395, topmargin + 105 + rowGap * 4);
                e.Graphics.DrawLine(mypen, leftmargin, topmargin + 100 + rowGap * 5, PrintPageWidth - leftmargin - rightmargin, topmargin + 100 + rowGap * 5);
                if (dgv.Name == "dataGridView1")
                {
                    bool havePages = DrawTestGrid(6, currentpageindex, e);
                    if (havePages)
                        e.HasMorePages = true;
                }
                else
                {
                    bool havePages = PrintDataGridData(6, currentpageindex, e);
                    if (havePages)
                        e.HasMorePages = true;
                }
            }
            else
            {
                if (dgv.Name == "dataGridView1")
                {
                    bool havePages = DrawTestGrid(0, currentpageindex, e);
                    if (havePages)
                        e.HasMorePages = true;
                }
                else
                {
                    bool havePages = PrintDataGridData(0, currentpageindex, e);
                    if (havePages)
                        e.HasMorePages = true;
                }
            }
            //绘制边框
            e.Graphics.DrawLine(mypen, leftmargin, topmargin + 100, PrintPageWidth - leftmargin - rightmargin, topmargin + 100);// 上边框
            e.Graphics.DrawLine(mypen, leftmargin, topmargin + 100, leftmargin, PrintPageHeight - topmargin - buttommargin); //左边框
            e.Graphics.DrawLine(mypen, leftmargin, PrintPageHeight - topmargin - buttommargin, PrintPageWidth - leftmargin - rightmargin,
                                            PrintPageHeight - topmargin - buttommargin); //下边框
            e.Graphics.DrawLine(mypen, PrintPageWidth - leftmargin - rightmargin, topmargin + 100, PrintPageWidth - leftmargin - rightmargin,
                                            PrintPageHeight - topmargin - buttommargin);// 右边框

        }

        public void PrintSingleTestData(PrintPageEventArgs e)
        {
            if (currentpageindex == 1)
            {
                rowGap = Convert.ToInt32((PrintPageHeight - topmargin - buttommargin - 5 * perpageRow) / perpageRow) + 3;
                //绘制标题与测试时间、人员信息
                e.Graphics.DrawString(deviceName + "设备测试记录", headFont, brush, 320, topmargin + 10);
                e.Graphics.DrawString("测试时间:   " + testTime, font, brush, 100, topmargin + 65);
                e.Graphics.DrawString(("测试员:   " + tester), font, brush, PrintPageWidth - leftmargin - rightmargin * 4, topmargin + 65);
                e.Graphics.DrawString("机车配属", font, brush, leftmargin + 5, topmargin + 105);
                e.Graphics.DrawLine(mypen, leftmargin + 78, topmargin + 100, leftmargin + 78, topmargin + 100 + rowGap); //
                e.Graphics.DrawString(deviceInfo.Cab_bur + " 铁路局 " + deviceInfo.Cab_seg + " 段 " + deviceInfo.Cab_pl + " 所  ", font, brush, leftmargin + 80, topmargin + 105);
                e.Graphics.DrawLine(mypen, leftmargin, topmargin + 100 + rowGap, PrintPageWidth - leftmargin - rightmargin, topmargin + 100 + rowGap);
                e.Graphics.DrawLine(mypen, leftmargin + 78, topmargin + 100 + rowGap, leftmargin + 78, topmargin + 100 + rowGap * 2);
                e.Graphics.DrawString("机车端别", font, brush, leftmargin + 5, topmargin + 100 + rowGap + 5);
                e.Graphics.DrawLine(mypen, leftmargin, topmargin + 100 + rowGap * 2, PrintPageWidth - leftmargin - rightmargin, topmargin + 100 + rowGap * 2);
                e.Graphics.DrawString(deviceInfo.Cab_db.ToString(), font, brush, leftmargin + 82, topmargin + 100 + rowGap + 5);
                e.Graphics.DrawLine(mypen, leftmargin + 138, topmargin + 100 + rowGap, leftmargin + 138, topmargin + 100 + rowGap * 2);
                e.Graphics.DrawString("机车编号", font, brush, leftmargin + 143, topmargin + 100 + rowGap + 5);
                e.Graphics.DrawLine(mypen, leftmargin + 216, topmargin + 100 + rowGap, leftmargin + 216, topmargin + 100 + rowGap * 2);
                e.Graphics.DrawString(deviceInfo.Cab_num, font, brush, leftmargin + 220, topmargin + 100 + rowGap + 5);
                e.Graphics.DrawLine(mypen, leftmargin + 370, topmargin + 100 + rowGap, leftmargin + 370, topmargin + 100 + rowGap * 2);
                e.Graphics.DrawString("机车修程", font, brush, leftmargin + 375, topmargin + 105 + rowGap * 1);
                e.Graphics.DrawLine(mypen, leftmargin + 450, topmargin + 100 + rowGap, leftmargin + 450, topmargin + 100 + rowGap * 2);
                e.Graphics.DrawString(deviceInfo.Cab_xc, font, brush, leftmargin + 455, topmargin + 105 + rowGap);
                e.Graphics.DrawString("CIR设备配属", font, brush, leftmargin + 5, topmargin + 105 + rowGap * 2);
                e.Graphics.DrawLine(mypen, leftmargin + 115, topmargin + 100 + rowGap * 2, leftmargin + 115, topmargin + 100 + rowGap * 3);
                e.Graphics.DrawLine(mypen, leftmargin, topmargin + 100 + rowGap * 3, PrintPageWidth - leftmargin - rightmargin, topmargin + 100 + rowGap * 3);
                e.Graphics.DrawString(deviceInfo.Dev_bur + " 铁路局 " + deviceInfo.Dev_seg + " 段 " + deviceInfo.Dev_ws + " 车间 " + deviceInfo.Dev_wa + " 工区  ",
                                                     font, brush, leftmargin + 120, topmargin + 105 + rowGap * 2);
                e.Graphics.DrawString("CIR设备型号", font, brush, leftmargin + 5, topmargin + 105 + rowGap * 3);
                e.Graphics.DrawLine(mypen, leftmargin + 115, topmargin + 100 + rowGap * 3, leftmargin + 115, topmargin + 100 + rowGap * 4);
                e.Graphics.DrawString(deviceInfo.Dev_mod, font, brush, leftmargin + 120, topmargin + 105 + rowGap * 3);
                e.Graphics.DrawLine(mypen, leftmargin + 232, topmargin + 100 + rowGap * 3, leftmargin + 232, topmargin + 100 + rowGap * 4);
                e.Graphics.DrawString("CIR设备生产商", font, brush, leftmargin + 235, topmargin + 105 + rowGap * 3);
                e.Graphics.DrawLine(mypen, leftmargin + 362, topmargin + 100 + rowGap * 3, leftmargin + 362, topmargin + 100 + rowGap * 4);
                e.Graphics.DrawString(deviceInfo.Dev_man, font, brush, leftmargin + 365, topmargin + 105 + rowGap * 3);
                e.Graphics.DrawLine(mypen, leftmargin, topmargin + 100 + rowGap * 4, PrintPageWidth - leftmargin - rightmargin, topmargin + 100 + rowGap * 4);
                e.Graphics.DrawString("CIR设备编号", font, brush, leftmargin + 5, topmargin + 105 + rowGap * 4);
                e.Graphics.DrawLine(mypen, leftmargin + 115, topmargin + 100 + rowGap * 4, leftmargin + 115, topmargin + 100 + rowGap * 5);
                e.Graphics.DrawString(deviceInfo.Dev_num, font, brush, leftmargin + 120, topmargin + 105 + rowGap * 4);
                e.Graphics.DrawLine(mypen, leftmargin + 275, topmargin + 100 + rowGap * 4, leftmargin + 275, topmargin + 100 + rowGap * 5);
                e.Graphics.DrawString("CIR设备修程", font, brush, leftmargin + 280, topmargin + 105 + rowGap * 4);
                e.Graphics.DrawLine(mypen, leftmargin + 390, topmargin + 100 + rowGap * 4, leftmargin + 390, topmargin + 100 + rowGap * 5);
                e.Graphics.DrawString(deviceInfo.Cab_xc, font, brush, leftmargin + 395, topmargin + 105 + rowGap * 4);
                e.Graphics.DrawLine(mypen, leftmargin, topmargin + 100 + rowGap * 5, PrintPageWidth - leftmargin - rightmargin, topmargin + 100 + rowGap * 5);
                if (dgv.Columns.Count ==2 )
                {
                    bool havePages = PrintSingleFunTrial(6, currentpageindex, e);
                    if (havePages)
                        e.HasMorePages = true;
                }
                else
                {
                    bool havePages = PrintDataGridData(6, currentpageindex, e);
                    if (havePages)
                        e.HasMorePages = true;
                }
            }
            else
            {
                if (dgv.Columns.Count == 2)
                {
                    bool havePages = PrintSingleFunTrial(0, currentpageindex, e);
                    if (havePages)
                        e.HasMorePages = true;
                }
                else
                {
                    bool havePages = PrintDataGridData(0, currentpageindex, e);
                    if (havePages)
                        e.HasMorePages = true;
                }
            }
            //绘制边框
            e.Graphics.DrawLine(mypen, leftmargin, topmargin + 100, PrintPageWidth - leftmargin - rightmargin, topmargin + 100);// 上边框
            e.Graphics.DrawLine(mypen, leftmargin, topmargin + 100, leftmargin, PrintPageHeight - topmargin - buttommargin); //左边框
            e.Graphics.DrawLine(mypen, leftmargin, PrintPageHeight - topmargin - buttommargin, PrintPageWidth - leftmargin - rightmargin,
                                            PrintPageHeight - topmargin - buttommargin); //下边框
            e.Graphics.DrawLine(mypen, PrintPageWidth - leftmargin - rightmargin, topmargin + 100, PrintPageWidth - leftmargin - rightmargin,
                                            PrintPageHeight - topmargin - buttommargin);// 右边框
        }

        private bool PrintDataGridData(int startdrawIndex, int currentPage, PrintPageEventArgs e)
        {
            if (dgv != null && dgv.Name != "dataGridView4")
            {
                endpageRow = currentPage * perpageRow;
               // colWidth = Convert.ToInt32((PrintPageWidth - leftmargin - rightmargin - 10) / 6);
                StringFormat format = new StringFormat();
                format.Alignment = StringAlignment.Center;
                format.LineAlignment = StringAlignment.Center;
                Rectangle rec = new Rectangle();
                int initY = rowGap * (startdrawIndex);
                recWidth = colWidth;
                recY = topmargin + 115 + initY;
                if (currentPage == 1)
                {
                    colWidth = Convert.ToInt32((PrintPageWidth - leftmargin - rightmargin - 10) / dgv.Columns.Count);
                    int startColumnIndex = 0;
                    int m = 0;
                    if (IsHaveCheck)
                        startColumnIndex = 1;
                    for (int k = startColumnIndex; k < dgv.Columns.Count; k++)
                    {
                        if (m == 0 || m == 1)
                            e.Graphics.DrawString(dgv.Columns[k].HeaderText, font, brush, leftmargin + m * colWidth + 10, topmargin + 75 + rowGap * (startdrawIndex));
                        else if (m == 2)
                            e.Graphics.DrawString(dgv.Columns[k].HeaderText, font, brush, leftmargin + m * colWidth + 40, topmargin + 75 + rowGap * (startdrawIndex));
                        else if (m == 3)
                            e.Graphics.DrawString(dgv.Columns[k].HeaderText, font, brush, leftmargin + m * colWidth +45, topmargin + 75 + rowGap * (startdrawIndex));
                        else if (m == 4)
                            e.Graphics.DrawString(dgv.Columns[k].HeaderText, font, brush, leftmargin + m * colWidth + 80, topmargin + 75 + rowGap * (startdrawIndex));
                        m++;
                    }
                    printedLine = 0;
                    haveToPageEnd = false;
                    for (int i = 0; i < dgv.Rows.Count; i++)
                    {
                        bool result = true;
                        if (dgv.Columns.Count > 3)
                            result = dgv.Rows[i].Cells["测试结果"].Value.ToString() != "" ? true : false;
                        else
                            result = dgv.Rows[i].Cells["试验结果"].Value.ToString() != "" ? true : false;
                        if (result)
                        {
                            m = 0;
                            for (int j = startColumnIndex; j < dgv.Columns.Count; j++)
                            {

                                if ((PrintPageWidth - leftmargin - rightmargin - m * colWidth) < colWidth)
                                    recWidth = PrintPageWidth - leftmargin - rightmargin - m * colWidth;
                                else
                                    recWidth = colWidth;
                                if (recY < e.MarginBounds.Height)
                                {
                                    if (m == 0)
                                        recX = leftmargin + m * colWidth + 10;  // 每个单元格开始和X坐标
                                    else if (m == 1)
                                        recX = leftmargin + m * colWidth + 10;
                                    else if (m == 2)
                                        recX = leftmargin + 5 + m * colWidth + 35;
                                    else if (m == 3)
                                        recX = leftmargin + 5 + m * colWidth + 45;
                                    else if (m == 4)
                                        recX = leftmargin + 5 + m * colWidth +60;
                                    recHeight = dgv.Rows[i].Height > 80 ? dgv.Rows[i].Height : 80;
                                  //  recHeight = 80;                      //每行的行高
                                    rec = new Rectangle(recX, recY, recWidth, recHeight);
                                    object str = dgv.Rows[i].Cells[j].Value;
                                    string valuStr = string.Empty;
                                    if (str != null)
                                    {
                                        valuStr = str.ToString();
                                    }
                                    e.Graphics.DrawString(valuStr, font, brush, rec, format);
                                    e.Graphics.DrawLine(mypen, leftmargin, recY - 3, PrintPageWidth - leftmargin - rightmargin, recY - 3);
                                }
                                else
                                {
                                    haveToPageEnd = true;
                                    break;
                                }
                                m++;
                            }
                            if (haveToPageEnd)
                                break;
                            else
                            {
                                recY = recY + (recHeight); //每行的Y 坐标
                                printedLine++;
                            }
                        }
                        else
                            continue;
                    }
                    if (printedLine < dgv.Rows.Count)
                    {
                        currentpageindex++;
                        if (dgv.Columns.Count > 4)
                        {
                            e.Graphics.DrawLine(mypen, leftmargin + colWidth + 10, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth + 10, PrintPageHeight - topmargin - buttommargin);
                            e.Graphics.DrawLine(mypen, leftmargin + colWidth * 2 + 40, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth * 2 + 40, PrintPageHeight - topmargin - buttommargin);
                            e.Graphics.DrawLine(mypen, leftmargin + colWidth * 3 + 45, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth * 3 + 45, PrintPageHeight - topmargin - buttommargin);
                            e.Graphics.DrawLine(mypen, leftmargin + colWidth * 4 + 80, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth * 4 + 80, PrintPageHeight - topmargin - buttommargin);
                        }
                        else
                        {
                            e.Graphics.DrawLine(mypen, leftmargin + colWidth + 10, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth + 10, PrintPageHeight - topmargin - buttommargin);
                            e.Graphics.DrawLine(mypen, leftmargin + colWidth * 2 + 40, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth * 2 + 40, PrintPageHeight - topmargin - buttommargin);
                            e.Graphics.DrawLine(mypen, leftmargin + colWidth * 3 + 45, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth * 3 + 45, PrintPageHeight - topmargin - buttommargin);
                        }
                        haveToPageEnd = false;
                        return true;
                    }
                    else
                    {
                        if (dgv.Columns.Count > 4)
                        {
                            e.Graphics.DrawLine(mypen, leftmargin + colWidth + 10, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth + 10, PrintPageHeight - topmargin - buttommargin);
                            e.Graphics.DrawLine(mypen, leftmargin + colWidth * 2 + 40, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth * 2 + 40, PrintPageHeight - topmargin - buttommargin);
                            e.Graphics.DrawLine(mypen, leftmargin + colWidth * 3 + 45, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth * 3 + 45, PrintPageHeight - topmargin - buttommargin);
                            e.Graphics.DrawLine(mypen, leftmargin + colWidth * 4 + 80, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth * 4 + 80, PrintPageHeight - topmargin - buttommargin);
                        }
                        else
                        {
                            e.Graphics.DrawLine(mypen, leftmargin + colWidth + 10, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth + 10, PrintPageHeight - topmargin - buttommargin);
                            e.Graphics.DrawLine(mypen, leftmargin + colWidth * 2 + 40, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth * 2 + 40, PrintPageHeight - topmargin - buttommargin);
                            e.Graphics.DrawLine(mypen, leftmargin + colWidth * 3 + 45, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth * 3 + 45, PrintPageHeight - topmargin - buttommargin);
                        } 
                        if (recY < e.MarginBounds.Height)
                            e.Graphics.DrawLine(mypen, leftmargin, recY - 3, PrintPageWidth - leftmargin - rightmargin, recY - 3);
                        haveToPageEnd = true;
                        return false;
                    }
                }
                else
                {
                    colWidth = Convert.ToInt32((PrintPageWidth - leftmargin - rightmargin - 10) / dgv.Columns.Count);
                    int m = 0;
                    int startColumnIndex = 0;
                    if (IsHaveCheck)
                        startColumnIndex = 1;
                    for (int k = startColumnIndex; k < dgv.Columns.Count; k++)
                    {
                        if (m == 0 || m == 1)
                            e.Graphics.DrawString(dgv.Columns[k].HeaderText, font, brush, leftmargin + m * colWidth + 10, topmargin + 105);
                        else if (m == 2)
                            e.Graphics.DrawString(dgv.Columns[k].HeaderText, font, brush, leftmargin + m * colWidth + 40, topmargin + 105);
                        else if (m == 3)
                            e.Graphics.DrawString(dgv.Columns[k].HeaderText, font, brush, leftmargin + m * colWidth +50, topmargin + 105);
                        else if (m == 4)
                            e.Graphics.DrawString(dgv.Columns[k].HeaderText, font, brush, leftmargin + m * colWidth + 80, topmargin + 105);
                        m++;
                    }
                    recY += 30;
                    for (int i = printedLine; i < dgv.Rows.Count; i++)
                    {
                        bool result = true;
                        if (dgv.Columns.Count > 3)
                            result = dgv.Rows[i].Cells["测试结果"].Value.ToString() != "" ? true : false;
                        else
                            result = dgv.Rows[i].Cells["试验结果"].Value.ToString() != "" ? true : false;
                        if (result)
                        {
                            m = 0;
                            for (int j = startColumnIndex; j < dgv.Columns.Count; j++)
                            {

                                if ((PrintPageWidth - leftmargin - rightmargin - m * colWidth) < colWidth)
                                    recWidth = PrintPageWidth - leftmargin - rightmargin - m * colWidth;
                                else
                                    recWidth = colWidth;
                                if (recY < e.MarginBounds.Height)
                                {
                                    if (m == 0)
                                        recX = leftmargin + m * colWidth + 10;  // 每个单元格开始和X坐标
                                    else if (m == 1)
                                        recX = leftmargin + m * colWidth + 10;
                                    else if (m == 2)
                                        recX = leftmargin + 5 + m * colWidth + 35;
                                    else if (m == 3)
                                        recX = leftmargin + 5 + m * colWidth + 45;
                                    else if (m == 4)
                                        recX = leftmargin + 5 + m * colWidth +60;
                                    recHeight = dgv.Rows[i].Height > 50 ? dgv.Rows[i].Height : 50;
                                    //recHeight = 80;
                                    rec = new Rectangle(recX, recY, recWidth, recHeight);
                                    string str = dgv.Rows[i].Cells[j].Value.ToString();
                                    e.Graphics.DrawString(str, font, brush, rec, format);
                                    e.Graphics.DrawLine(mypen, leftmargin, recY - 3, PrintPageWidth - leftmargin - rightmargin, recY - 3);
                                }
                                else
                                {
                                    haveToPageEnd = true;
                                    break;
                                }
                                m++;
                            }
                            if (haveToPageEnd)
                                break;
                            else
                            {
                                recY += recHeight;
                                printedLine++;
                            }
                        }
                        else
                            continue;
                    }
                    if (recY < e.MarginBounds.Height)
                    {
                        e.Graphics.DrawLine(mypen, leftmargin, recY - 3, PrintPageWidth - leftmargin - rightmargin, recY - 3);
                    }
                    //绘制每列列线
                    if (dgv.Columns.Count > 4)
                    {
                        e.Graphics.DrawLine(mypen, leftmargin + colWidth + 10, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth + 10, PrintPageHeight - topmargin - buttommargin);
                        e.Graphics.DrawLine(mypen, leftmargin + colWidth * 2 + 40, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth * 2 + 40, PrintPageHeight - topmargin - buttommargin);
                        e.Graphics.DrawLine(mypen, leftmargin + colWidth * 3 + 45, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth * 3 + 45, PrintPageHeight - topmargin - buttommargin);
                        e.Graphics.DrawLine(mypen, leftmargin + colWidth * 4 + 80, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth * 4 + 80, PrintPageHeight - topmargin - buttommargin);
                    }
                    else
                    {
                        e.Graphics.DrawLine(mypen, leftmargin + colWidth + 10, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth + 10, PrintPageHeight - topmargin - buttommargin);
                        e.Graphics.DrawLine(mypen, leftmargin + colWidth * 2 + 40, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth * 2 + 40, PrintPageHeight - topmargin - buttommargin);
                        e.Graphics.DrawLine(mypen, leftmargin + colWidth * 3 + 45, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth * 3 + 45, PrintPageHeight - topmargin - buttommargin);
                    } 
                    if (printedLine < dgv.Rows.Count - 1)
                    {
                        currentpageindex++;
                        haveToPageEnd = false;
                        return true;
                    }
                    else
                    {
                        haveToPageEnd = true;
                        return false;
                    }
                }
            }
            else
            {
                endpageRow = currentPage * perpageRow;
                StringFormat format = new StringFormat();
                format.Alignment = StringAlignment.Center;
                format.LineAlignment = StringAlignment.Center;
                Rectangle rec = new Rectangle();
                int initY = rowGap * (startdrawIndex);
                recY = topmargin + 115 + initY;
                if (currentPage == 1)
                {
                    colWidth = Convert.ToInt32((PrintPageWidth - leftmargin - rightmargin - 10) / dgv.Columns.Count);
                    int startColumnIndex = 0;
                    int m = 0;
                    if (IsHaveCheck)
                        startColumnIndex = 1;
                    for (int k = startColumnIndex; k < dgv.Columns.Count; k++)
                    {
                        if (m == 0 || m == 1)
                            e.Graphics.DrawString(dgv.Columns[k].HeaderText, font, brush, leftmargin + m * colWidth + 10, topmargin + 75 + rowGap * (startdrawIndex));
                        if (m == 2)
                            e.Graphics.DrawString(dgv.Columns[k].HeaderText, font, brush, leftmargin + m * colWidth + 40, topmargin + 75 + rowGap * (startdrawIndex));
                        m++;
                    }
                    printedLine = 0;
                    haveToPageEnd = false;
                    for (int i = 0; i < dgv.Rows.Count; i++)
                    {
                        bool result = true;
                        result = dgv.Rows[i].Cells["试验结果"].Value.ToString() != "" ? true : false;
                        if (result)
                        {
                            m = 0;
                            for (int j = startColumnIndex; j < dgv.Columns.Count; j++)
                            {

                                if ((PrintPageWidth - leftmargin - rightmargin - m * colWidth) < colWidth)
                                    recWidth = PrintPageWidth - leftmargin - rightmargin - m * colWidth;
                                else
                                    recWidth = colWidth;
                                if (recY < e.MarginBounds.Height)
                                {
                                    if (m == 0)
                                        recX = leftmargin + m * colWidth + 10;  // 每个单元格开始和X坐标
                                    else if (m == 1)
                                        recX = leftmargin + m * colWidth + 10;
                                    else if (m == 2)
                                        recX = leftmargin + m * colWidth + 10;
                                    recHeight = dgv.Rows[i].Height > 50 ? dgv.Rows[i].Height : 50;
                                  //  recHeight = 80;                      //每行的行高
                                    rec = new Rectangle(recX, recY, recWidth, recHeight);
                                    object str = dgv.Rows[i].Cells[j].Value;
                                    string valuStr = string.Empty;
                                    if (str != null)
                                    {
                                        valuStr = str.ToString();
                                    }
                                    e.Graphics.DrawString(valuStr, font, brush, rec, format);
                                    e.Graphics.DrawLine(mypen, leftmargin, recY - 3, PrintPageWidth - leftmargin - rightmargin, recY - 3);
                                }
                                else
                                {
                                    haveToPageEnd = true;
                                    break;
                                }
                                m++;
                            }
                            if (haveToPageEnd)
                                break;
                            else
                            {
                                recY = recY + (recHeight); //每行的Y 坐标
                                printedLine++;
                            }
                        }
                        else
                            continue;
                    }
                    if (printedLine < dgv.Rows.Count)
                    {
                        currentpageindex++;
                        e.Graphics.DrawLine(mypen, leftmargin + colWidth + 10, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth + 10, PrintPageHeight - topmargin - buttommargin);
                        e.Graphics.DrawLine(mypen, leftmargin + colWidth * 2 + 40, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth * 2 + 40, PrintPageHeight - topmargin - buttommargin);
                        haveToPageEnd = false;
                        return true;
                    }
                    else
                    {
                        e.Graphics.DrawLine(mypen, leftmargin + colWidth + 10, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth + 10, PrintPageHeight - topmargin - buttommargin);
                        e.Graphics.DrawLine(mypen, leftmargin + colWidth * 2 + 40, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth * 2 + 40, PrintPageHeight - topmargin - buttommargin);
                        if (recY < e.MarginBounds.Height)
                            e.Graphics.DrawLine(mypen, leftmargin, recY - 3, PrintPageWidth - leftmargin - rightmargin, recY - 3);
                        haveToPageEnd = true;
                        return false;
                    }
                }
                else
                {
                    colWidth = Convert.ToInt32((PrintPageWidth - leftmargin - rightmargin - 10) / dgv.Columns.Count);
                    int m = 0;
                    int startColumnIndex = 0;
                    if (IsHaveCheck)
                        startColumnIndex = 1;
                    for (int k = startColumnIndex; k < dgv.Columns.Count; k++)
                    {
                        if (m == 0 || m == 1)
                            e.Graphics.DrawString(dgv.Columns[k].HeaderText, font, brush, leftmargin + m * colWidth + 10, topmargin + 105);
                        else if (m == 2)
                            e.Graphics.DrawString(dgv.Columns[k].HeaderText, font, brush, leftmargin + m * colWidth + 40, topmargin + 105);
                        m++;
                    }
                    recY += 30;
                    for (int i = printedLine + 1; i < dgv.Rows.Count; i++)
                    {
                        bool result = true;
                         result = dgv.Rows[i].Cells["试验结果"].Value.ToString() != "" ? true : false;
                        if (result)
                        {
                            m = 0;
                            for (int j = startColumnIndex; j < dgv.Columns.Count; j++)
                            {

                                if ((PrintPageWidth - leftmargin - rightmargin - m * colWidth) < colWidth)
                                    recWidth = PrintPageWidth - leftmargin - rightmargin - m * colWidth;
                                else
                                    recWidth = colWidth;
                                if (recY < e.MarginBounds.Height)
                                {
                                    if (m == 0)
                                        recX = leftmargin + m * colWidth + 10;  // 每个单元格开始和X坐标
                                    else if (m == 1)
                                        recX = leftmargin + m * colWidth + 10;
                                    else if (m == 2)
                                        recX = leftmargin + m * colWidth + 10;
                                    recHeight = dgv.Rows[i].Height > 50 ? dgv.Rows[i].Height : 50;
                                   // recHeight = 80;
                                    rec = new Rectangle(recX, recY, recWidth, recHeight);
                                    string str = dgv.Rows[i].Cells[j].Value.ToString();
                                    e.Graphics.DrawString(str, font, brush, rec, format);
                                    e.Graphics.DrawLine(mypen, leftmargin, recY - 3, PrintPageWidth - leftmargin - rightmargin, recY - 3);
                                }
                                else
                                {
                                    haveToPageEnd = true;
                                    break;
                                }
                                m++;
                            }
                            if (haveToPageEnd)
                                break;
                            else
                            {
                                recY += recHeight;
                                printedLine++;
                            }
                        }
                        else
                            continue;
                    }
                    if (recY < e.MarginBounds.Height)
                    {
                        e.Graphics.DrawLine(mypen, leftmargin, recY - 3, PrintPageWidth - leftmargin - rightmargin, recY - 3);
                    }
                    //绘制每列列线
                    e.Graphics.DrawLine(mypen, leftmargin + colWidth + 10, topmargin + 100 + rowGap * (startdrawIndex), leftmargin + colWidth + 10, PrintPageHeight - topmargin - buttommargin);
                    e.Graphics.DrawLine(mypen, leftmargin + colWidth * 2 + 40, topmargin + 100 + rowGap * (startdrawIndex), leftmargin + colWidth * 2 + 40, PrintPageHeight - topmargin - buttommargin);
                    if (printedLine < dgv.Rows.Count - 1)
                    {
                        currentpageindex++;
                        haveToPageEnd = false;
                        return true;
                    }
                    else
                    {
                        haveToPageEnd = true;
                        return false;
                    }
                }
            }
        }

        private bool PrintSingleFunTrial(int startdrawIndex, int currentPage, PrintPageEventArgs e)
        {
            endpageRow = currentPage * perpageRow;
            StringFormat format = new StringFormat();
            format.Alignment = StringAlignment.Center;
            format.LineAlignment = StringAlignment.Center;
            Rectangle rec = new Rectangle();
            int initY = rowGap * (startdrawIndex);
            recY = topmargin + 115 + initY;
            if (currentPage == 1)
            {
                colWidth = Convert.ToInt32((PrintPageWidth - leftmargin - rightmargin - 10) / dgv.Columns.Count);
                int startColumnIndex = 0;
                int m = 0;
                if (IsHaveCheck)
                    startColumnIndex = 1;
                for (int k = startColumnIndex; k < dgv.Columns.Count; k++)
                {
                    if (m == 0 || m == 1)
                        e.Graphics.DrawString(dgv.Columns[k].HeaderText, font, brush, leftmargin + m * colWidth + 10, topmargin + 75 + rowGap * (startdrawIndex));
                    if (m == 2)
                        e.Graphics.DrawString(dgv.Columns[k].HeaderText, font, brush, leftmargin + m * colWidth + 40, topmargin + 75 + rowGap * (startdrawIndex));
                    m++;
                }
                printedLine = 0;
                haveToPageEnd = false;
                for (int i = 0; i < dgv.Rows.Count; i++)
                {
                    bool result = true;
                    result = dgv.Rows[i].Cells["试验结果"].Value.ToString() != "" ? true : false;
                    if (result)
                    {
                        m = 0;
                        for (int j = startColumnIndex; j < dgv.Columns.Count; j++)
                        {

                            if ((PrintPageWidth - leftmargin - rightmargin - m * colWidth) < colWidth)
                                recWidth = PrintPageWidth - leftmargin - rightmargin - m * colWidth;
                            else
                                recWidth = colWidth;
                            if (recY < e.MarginBounds.Height)
                            {
                                if (m == 0)
                                    recX = leftmargin + m * colWidth + 10;  // 每个单元格开始和X坐标
                                else if (m == 1)
                                    recX = leftmargin + m * colWidth + 10;
                                else if (m == 2)
                                    recX = leftmargin + m * colWidth + 10;
                                recHeight = dgv.Rows[i].Height > 50 ? dgv.Rows[i].Height : 50;
                               // recHeight = 80;                      //每行的行高
                                rec = new Rectangle(recX, recY, recWidth, recHeight);
                                object str = dgv.Rows[i].Cells[j].Value;
                                string valuStr = string.Empty;
                                if (str != null)
                                {
                                    valuStr = str.ToString();
                                }
                                e.Graphics.DrawString(valuStr, font, brush, rec, format);
                                e.Graphics.DrawLine(mypen, leftmargin, recY - 3, PrintPageWidth - leftmargin - rightmargin, recY - 3);
                            }
                            else
                            {
                                haveToPageEnd = true;
                                break;
                            }
                            m++;
                        }
                        if (haveToPageEnd)
                            break;
                        else
                        {
                            recY = recY + (recHeight); //每行的Y 坐标
                            printedLine++;
                        }
                    }
                    else
                        continue;
                }
                if (printedLine < dgv.Rows.Count)
                {
                    currentpageindex++;
                    e.Graphics.DrawLine(mypen, leftmargin + colWidth + 10, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth + 10, PrintPageHeight - topmargin - buttommargin);
                   // e.Graphics.DrawLine(mypen, leftmargin + colWidth * 2 + 40, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth * 2 + 40, PrintPageHeight - topmargin - buttommargin);
                    haveToPageEnd = false;
                    return true;
                }
                else
                {
                    e.Graphics.DrawLine(mypen, leftmargin + colWidth + 10, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth + 10, PrintPageHeight - topmargin - buttommargin);
                  //  e.Graphics.DrawLine(mypen, leftmargin + colWidth * 2 + 40, topmargin + 100 + rowGap * (startdrawIndex - 1), leftmargin + colWidth * 2 + 40, PrintPageHeight - topmargin - buttommargin);
                    if (recY < e.MarginBounds.Height)
                        e.Graphics.DrawLine(mypen, leftmargin, recY - 3, PrintPageWidth - leftmargin - rightmargin, recY - 3);
                    haveToPageEnd = true;
                    return false;
                }
            }
            else
            {
                colWidth = Convert.ToInt32((PrintPageWidth - leftmargin - rightmargin - 10) / dgv.Columns.Count);
                int m = 0;
                int startColumnIndex = 0;
                if (IsHaveCheck)
                    startColumnIndex = 1;
                for (int k = startColumnIndex; k < dgv.Columns.Count; k++)
                {
                    if (m == 0 || m == 1)
                        e.Graphics.DrawString(dgv.Columns[k].HeaderText, font, brush, leftmargin + m * colWidth + 10, topmargin + 105);
                    else if (m == 2)
                        e.Graphics.DrawString(dgv.Columns[k].HeaderText, font, brush, leftmargin + m * colWidth + 40, topmargin + 105);
                    m++;
                }
                recY += 30;
                for (int i = printedLine + 1; i < dgv.Rows.Count; i++)
                {
                    bool result = true;
                    result = dgv.Rows[i].Cells["试验结果"].Value.ToString() != "" ? true : false;
                    if (result)
                    {
                        m = 0;
                        for (int j = startColumnIndex; j < dgv.Columns.Count; j++)
                        {

                            if ((PrintPageWidth - leftmargin - rightmargin - m * colWidth) < colWidth)
                                recWidth = PrintPageWidth - leftmargin - rightmargin - m * colWidth;
                            else
                                recWidth = colWidth;
                            if (recY < e.MarginBounds.Height)
                            {
                                if (m == 0)
                                    recX = leftmargin + m * colWidth + 10;  // 每个单元格开始和X坐标
                                else if (m == 1)
                                    recX = leftmargin + m * colWidth + 10;
                                else if (m == 2)
                                    recX = leftmargin + m * colWidth + 10;
                                recHeight = dgv.Rows[i].Height > 50 ? dgv.Rows[i].Height : 50;
                               // recHeight = 80;
                                rec = new Rectangle(recX, recY, recWidth, recHeight);
                                string str = dgv.Rows[i].Cells[j].Value.ToString();
                                e.Graphics.DrawString(str, font, brush, rec, format);
                                e.Graphics.DrawLine(mypen, leftmargin, recY - 3, PrintPageWidth - leftmargin - rightmargin, recY - 3);
                            }
                            else
                            {
                                haveToPageEnd = true;
                                break;
                            }
                            m++;
                        }
                        if (haveToPageEnd)
                            break;
                        else
                        {
                            recY += recHeight;
                            printedLine++;
                        }
                    }
                    else
                        continue;
                }
                if (recY < e.MarginBounds.Height)
                {
                    e.Graphics.DrawLine(mypen, leftmargin, recY - 3, PrintPageWidth - leftmargin - rightmargin, recY - 3);
                }
                //绘制每列列线
                e.Graphics.DrawLine(mypen, leftmargin + colWidth + 10, topmargin + 100 + rowGap * (startdrawIndex), leftmargin + colWidth + 10, PrintPageHeight - topmargin - buttommargin);
              //  e.Graphics.DrawLine(mypen, leftmargin + colWidth * 2 + 40, topmargin + 100 + rowGap * (startdrawIndex), leftmargin + colWidth * 2 + 40, PrintPageHeight - topmargin - buttommargin);
                if (printedLine < dgv.Rows.Count - 1)
                {
                    currentpageindex++;
                    haveToPageEnd = false;
                    return true;
                }
                else
                {
                    haveToPageEnd = true;
                    return false;
                }
            }
        }
    }
}
